package com.hydl.power.service.impl;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.TransformerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TransformerServiceImpl implements TransformerService {

    @Autowired
    private TransformerMapper transformerMapper;
    @Autowired
    private ProjectMapper ProjectMapper;
    @Autowired
    private CityMapper CityMapper;
    @Autowired
    private PowerRoomMapper PowerRoomMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    public PageInfo<Transformer> selectByCondition(Integer pageNum, Integer pageSize, String cityId, String projectId,String roomId) {
        List<Transformer> list = transformerMapper.selectByCondition(pageNum,pageSize,cityId,projectId,roomId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Transformer> selectByToken(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String token) {
        List<Transformer> list = transformerMapper.selectByToken(pageNum,pageSize,cityId,projectId,roomId,token);
        return new PageInfo<>(list);
    }

    @Override
    public int addTransformer(String roomId, String transformerName, String projectId, String cityId,String deviceId,String transformerCapacity) {
        Project project=ProjectMapper.queryProjectByProId(projectId);
        PowerRoom powerRoom=PowerRoomMapper.selectByRoomId(roomId);
        City city = CityMapper.queryCityByCityId(cityId);
        Device device=deviceMapper.getDeviceByDeviceId(deviceId);
        Transformer transformer= new Transformer();
        transformer.setCityId(cityId);
        transformer.setCityName(city.getCityName());
        transformer.setProjectId(projectId);
        transformer.setProjectName(project.getProjectName());
        transformer.setRoomId(roomId);
        transformer.setRoomName(powerRoom.getRoomName());
        transformer.setTransformerName(transformerName);
        transformer.setDeviceId(deviceId);
        transformer.setDeviceName(device.getDeviceName());
        transformer.setTransformerCapacity(transformerCapacity);
        return transformerMapper.addTransformer(transformer);
    }

    @Override
    public int deleteById(Integer id) {
        return transformerMapper.deleteById(id);
    }

    @Override
    public int updateTrans(String transformerName, Integer id) {
        return transformerMapper.updateTrans(transformerName,id);
    }

    @Override
    public Transformer selectById(Integer id) {
        return transformerMapper.selectById(id);
    }

    @Override
    public PageInfo<Transformer> queryTransformerLike(Integer pageNum,Integer pageSize,String text, String token) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null){
            if(sysUser.getStatus()==2) {
                return new PageInfo<>(transformerMapper.queryTransformerLike(pageNum,pageSize,text,sysUser.getProjectId().toString()));
            }
        }
        return new PageInfo<>(transformerMapper.queryTransformerLike(pageNum,pageSize,text,null));
    }
}
